package sitesettinghandler

import (
	"github.com/gin-gonic/gin"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/ratingsetting"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
)

//评级配置列表
func RatingSettingList(c *gin.Context) {
	param := &struct {
		BaseListReq request.BaseListReq
	}{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	//获取数据
	data := dao.NewSiteSettingDao().RatingSettingListDao(&param.BaseListReq)
	//错误处理
	if data.Err != nil {
		Response(c)("", CodeInternalServerError, data.Data)(ServerError)
	}

	Response(c)("", CodeSuccess, ResponseList{
		PageNo:     param.BaseListReq.PageNo,
		PageSize:   param.BaseListReq.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  0,
		Data:       data.Unwrap(),
	})(OK)
}

//评级配置编辑
func RatingSettingEdit(c *gin.Context) {
	param := &ratingsetting.RatingSetting{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	Response(c)("修改成功", CodeUpdateSuccess, dao.NewSiteSettingDao().RatingSettingEditDao(param.Id, param).Unwrap())(OK)
}

//评级配置新增
func RatingSettingSave(c *gin.Context) {
	param := &ratingsetting.RatingSetting{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()

	if param.Rating == "" {
		Response(c)("新增失败", CodeInvalidParam, false)(OK)
	}

	isRepeat := dao.NewSiteSettingDao().GetRatingSetting(param.Rating).Unwrap()
	if isRepeat == false {
		Response(c)("新增失败，该评级已存在", CodeInvalidParam, nil)(OK)
		return
	}

	Response(c)("新增成功", CodeUpdateSuccess, dao.NewSiteSettingDao().RatingSettingSaveDao(param).Unwrap())(OK)
}

//评级配置删除
func RatingSettingDelete(c *gin.Context) {
	param := &struct {
		Id int `json:"id"`
	}{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	Response(c)("删除成功", CodeUpdateSuccess, dao.NewSiteSettingDao().RatingSettingDeleteDao(param.Id).Unwrap())(OK)
}
